/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bugsandcritters;


import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.util.Random;
import java.util.ArrayList;

/**
 *
 * @author Robert Gibbons
 */
public class Bug extends GameObject{
    
    public double Size= 0;
    public static BufferedImage BugImage[] = new BufferedImage[4];
    public static Boolean NeedsLoading = true;
    public BufferedImage CurrentImage;
    
    public Boolean AttackingCritter = false;
    public double ChasedByCritter = 0;
    public static int DeadlyBugNeighbors = 2;
    
    public double FeedingTime=0;
    public static double BugFeedingTime = 30;
    
    public int Seeds = 0;
    
    public static double BugSeedingTime = 5;
    public static int FlowersForNewBug = 5;
    
    public int NewBugFlowers = 0;
    
    public double SeedingTime = 0;
    
    public Point TestPoint= new Point();
    public ArrayList<Flower> FlowersEaten =  new ArrayList<Flower>();
    
    @Override
    public void FindNewTarget()
    {
        
        
        Random r = new Random();
        int BestFacing = r.nextInt(4);
        Boolean IsCritter = false;
        Boolean ShouldMove = true;
        
        for(int i=0; i<4 ;i++)
        {
            this.AttackingCritter = false;
            
            TestPoint.FromFacing(i, X, Y);
            GameObject g = GridWorld.CheckFree(TestPoint.X, TestPoint.Y, this);
            if(g instanceof Critter)
            {
                
                IsCritter= true;
                BestFacing = TestPoint.InvertFacing(i);
                
                int BugCount = 0;
                for(int b=0; b<4; b++)
                {
                    TestPoint.FromFacing(b, g.X, g.Y);
                    
                    if(GridWorld.CheckFree(TestPoint.X, TestPoint.Y, g) 
                            instanceof Bug)
                        BugCount++;
                    
                    if(BugCount >= DeadlyBugNeighbors)
                    {
                        BestFacing = i;
                        this.AttackingCritter = true;
                    }
                }
                break;
            }
        }
        
        if(IsCritter && !AttackingCritter)
        {
            if(GridWorld.CheckFree(X, Y, this) instanceof Rock ) 
                ShouldMove = false;
            else
                for(int i=0; i<4 ;i++)
                {
                    TestPoint.FromFacing(i, X, Y);
                    if(GridWorld.CheckFree(TestPoint.X, TestPoint.Y, this) 
                            instanceof Rock)
                    {
                        BestFacing = i;
                        break;
                    }
                }
        }
        if(ShouldMove)
        {
            for(int i=0; i<4 ;i++)
            {
                TestPoint.FromFacing(BestFacing, X, Y);
                if((GridWorld.CheckFree(TestPoint.X, TestPoint.Y, this) 
                        instanceof Bug))
                    BestFacing = TestPoint.AddFacing(BestFacing);
            }

            TestPoint.FromFacing(BestFacing, X, Y);


            if(!(GridWorld.CheckFree(TestPoint.X, TestPoint.Y, this) 
                instanceof Bug))
                {
                    TargetX=TestPoint.X;
                    TargetY=TestPoint.Y;
                    CurrentImage = BugImage[BestFacing];
                }
        }
        if(IsCritter)
        ChasedByCritter = 10;
    }
    
    
    
    @Override
    public void Update()
    {
        //System.out.println("Bug Update");
        
        ChasedByCritter -= DeltaTime/1000000000.0;
        FeedingTime += DeltaTime/1000000000.0;
        if(FeedingTime>BugFeedingTime && Seeds == 0)
        {
            Size -= DeltaTime/4000000000.0;
            if(Size<0)
                this.Destroy();
        }
        else
        {
            Boolean Move = false;
            Size += DeltaTime/4000000000.0;
            if(Size>1)
            {
                Size=1;
                Move = true;
            }
            
            GameObject g = GridWorld.CheckFree(X, Y, this);
            if(g instanceof Flower  && ChasedByCritter < 0)
            {
                Flower f = (Flower)g;
                if(f.Size > 0.9 || FlowersEaten.contains(f))
                {
                    Move = false;
                    f.FlowerLifeTime = -1;
                    this.FeedingTime = 0;
                    if(!FlowersEaten.contains(f))
                    {
                        FlowersEaten.add(f);
                        Seeds += 2;
                        this.NewBugFlowers++;
                    }
                }
            }
            if(g instanceof Critter)
                Move=false;
            if(Move)
            {
                if(Seeds>0)
                {
                    SeedingTime += DeltaTime/1000000000.0;
                    if(SeedingTime > BugSeedingTime)
                        {
                            if(NewBugFlowers<FlowersForNewBug)
                            {
                                Seeds--;
                                SeedingTime=0;
                                GridWorld.AddChild(new Flower(),FromX,FromY);
                            }
                            else
                            {
                                NewBugFlowers = 0;
                                SeedingTime=0;
                                GridWorld.AddChild(new Bug(),FromX,FromY);
                            }
                        }
                }
                else
                    FlowersEaten.clear();
                
                super.Update();
            }
        }
    }
    
    @Override
    public void Draw(Graphics g)
    {
        if(NeedsLoading)
        {
            try
            {
                BugImage[0] = ImageIO.read(new File("BugImageL.png"));
                BugImage[1] = ImageIO.read(new File("BugImageU.png"));
                BugImage[2] = ImageIO.read(new File("BugImageR.png"));
                BugImage[3] = ImageIO.read(new File("BugImageD.png"));
                NeedsLoading = false;
            }
            catch(Exception e)
            { 
                System.out.println("Error Loading Image: \n"
                    +e.getMessage()); 
            }
        }
        else
        {
            
            if(CurrentImage == null)
                CurrentImage = BugImage[1];
            
            BufferedImage image = CurrentImage;
            if(image != null)
            g.drawImage(image, (int)(X- image.getWidth()/2.0*Size), 
                    (int)(Y- image.getHeight()/2.0*Size),
                    (int)(Size * image.getWidth()),
                    (int)(Size * image.getHeight()),
                    null);
            
            
        }
    }
}
